package cn.hxzy.course.service.impl;

import cn.hxzy.common.domain.*;
import cn.hxzy.common.domain.vo.ChpSecListVo;
import cn.hxzy.common.domain.vo.CourseListVo;
import cn.hxzy.common.domain.vo.CourseVo;
import cn.hxzy.common.domain.vo.CourseVoList;
import cn.hxzy.course.mapper.*;
import cn.hxzy.course.service.CourseService;
import cn.hxzy.common.util.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author niuyi
 * @since 2024-06-22
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private DirectionMapper directionMapper;
    @Autowired
    private ProfessionalMapper professionalMapper;
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private SectionMapper sectionMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private UserClassMapper userClassMapper;

//    @Autowired
//    private TaskMengerClient taskMengerClient;
@Override
public List<CourseVoList> getList(Integer courseId) {
    Integer schoolId = ThreadLocalUtil.getUser().getSchoolId();
    List<CourseVoList> course = courseMapper.getList(courseId,schoolId);
    for (CourseVoList course1:course){
        String directionId = course1.getDirectionId();
        String[] split = directionId.split(",");
        List<String> list = Arrays.asList(split);
        List<Direction> directions = directionMapper.selectBatchIds(list);
        List<String> collect = directions.stream().map(direction -> direction.getDirectionName()).collect(Collectors.toList());
        course1.setDirectionName(collect);
    }
    return course;
}

    @Override
    public List<CourseVoList> getUserIdList() {
        Integer userId = Math.toIntExact(ThreadLocalUtil.getUser().getUserId());
        List<CourseVoList> course = courseMapper.getUserIdList(userId);
        for (CourseVoList course1:course){
            String directionId = course1.getDirectionId();
            String[] split = directionId.split(",");
            List<String> list = Arrays.asList(split);
            List<Direction> directions = directionMapper.selectBatchIds(list);
            List<String> collect = directions.stream().map(direction -> direction.getDirectionName()).collect(Collectors.toList());
            course1.setDirectionName(collect);
        }

        return course;
    }


    @Override
    public List<CourseVoList> selectCourceListByName(String courseName) {
        List<CourseVoList> course = courseMapper.selectCourceListByName(courseName);
        for (CourseVoList course1:course){
            String directionId = course1.getDirectionId();
            String[] split = directionId.split(",");
            List<String> list = Arrays.asList(split);
            List<Direction> directions = directionMapper.selectBatchIds(list);
            List<String> collect = directions.stream().map(direction -> direction.getDirectionName()).collect(Collectors.toList());
            course1.setDirectionName(collect);
        }

        return course;
    }


    @Override
    public List<CourseVoList> selectListDirectionId(String directionId) {
        Direction direction = directionMapper.selectById(directionId);
        List<CourseVoList> course = courseMapper.selectListDirectionId(directionId);
       return course.stream().filter(c->{
           String directionIds = c.getDirectionId();
           String[] split = directionIds.split(",");
           List<String> list = Arrays.asList(split);
           List<Direction> directions = directionMapper.selectBatchIds(list);
           List<String> collect = directions.stream().map(direction1 -> direction.getDirectionName()).collect(Collectors.toList());
           c.setDirectionName(collect);

            List<String> directionName = c.getDirectionName();
            boolean contains = directionName.contains(direction.getDirectionName());
            return contains;
        }).collect(Collectors.toList());
//        return course;
    }
    @Override
    public List<CourseVoList> getFilteredCoursesByCourseNameAndDirection(String courseName, String directionId) {

        return Collections.emptyList();
    }




    @Override
    public Course getCourseById(int courseId) {
        Course course = courseMapper.selectById(courseId);
        if (course != null) {
            return course;
        }
        return null;
    }

    @Override
    public int updateCourse(Course course) {
        int i = courseMapper.updateById(course);
        return i;
    }

    @Override
    public List<ChpSecListVo> ChpSecListVo(Integer courseId) {
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        List<Chapter> chapters = chapterMapper.selectList(queryWrapper);

        List<ChpSecListVo> list = new ArrayList<>();
        if (chapters.size() > 0) {
            for (Chapter chapter : chapters) {
                ChpSecListVo chpSecListVo = new ChpSecListVo();
                String chapterName = chapter.getChapterName();
                Integer chapterId = chapter.getChapterId();
                QueryWrapper<Section> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("chapter_id", chapterId);
                List<Section> sections = sectionMapper.selectList(queryWrapper1);
                chpSecListVo.setCourseId(courseId);
                chpSecListVo.setChapterId(chapterId);
                chpSecListVo.setChapterName(chapterName);
                chpSecListVo.setSectionList(sections);
                list.add(chpSecListVo);
            }
        }
        return list;
    }

    @Override
    public int deleteChapter(Integer chapterId) {
        QueryWrapper<Section> queryWrapper = new QueryWrapper();
        queryWrapper.eq("chapter_id", chapterId);
        List<Section> sections = sectionMapper.selectList(queryWrapper);
        for (Section section : sections) {
            sectionMapper.deleteById(section.getSectionId());
        }
        int i = chapterMapper.deleteById(chapterId);
        return i;
   }




    @Override
    public int saveCourse(Course course) {
            int i = courseMapper.insert(course);
            if (i>0){
                return i;
            }
            return -1;
        }

    @Override
    public List<Course> selectAll() {
//        根据登录用户取到学校编号，班级编号
        Integer schoolId = ThreadLocalUtil.getUser().getSchoolId();
        //不是学生就直接查学校的所有课程
        if (Integer.parseInt(ThreadLocalUtil.getUser().getUserType()) != 2) {
            List<Course> courses = courseMapper.selectList(new QueryWrapper<Course>().eq("school_id", schoolId));
            return courses;
        }
        //查到是哪个班级
        Integer classId = userClassMapper.selectClassIdByUserId(ThreadLocalUtil.getUser().getUserId());
        return courseMapper.selectByClassId(classId);
    }

    @Override
    public List<CourseListVo> getCourseList() {
        //根据登录用户取到学校编号，班级编号
        Integer schoolId = ThreadLocalUtil.getUser().getSchoolId();
        //看登陆的是学生还是老师
        String userType = ThreadLocalUtil.getUser().getUserType();
        List<CourseListVo> listVos = new ArrayList<>();
        //如果是学生
        if (Integer.parseInt(userType) == 2) {
            //查到是哪个班级
            Integer classId = userClassMapper.selectClassIdByUserId(ThreadLocalUtil.getUser().getUserId());
            List<Course> courses = courseMapper.selectByClassId(classId);
            if (courses.size() > 0) {
                List<Integer> arrs = new ArrayList<>();
                //得到所有的方向
                for (int i = 0; i < courses.size(); i++) {
                    String arr[] = courses.get(i).getDirectionId().split(",");
                    for (int i1 = 0; i1 < arr.length; i1++) {
                        int i2 = Integer.parseInt(arr[i1]);
                        arrs.add(i2);
                    }
                }
                // 使用 HashSet 去重
                Set<Integer> uniqueDirections = new HashSet<>(arrs);
                // 去重后的集合转换回 List
                List<Integer> arrayList = new ArrayList<>(uniqueDirections);
                for (int i = 0; i < arrayList.size(); i++) {
                    //声明对象
                    CourseListVo courseListVo = new CourseListVo();
                    Direction direction = directionMapper.selectById(arrayList.get(i));
                    //课程集合
                    List<CourseVo> list = new ArrayList<>();
                    //查此班级这个方向的课程
                    List<Course> courses1 = courseMapper.selectListByDid(direction.getDirectionId(),classId);
                    //查到的课程
                    for (int i1 = 0; i1 < courses1.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs2 = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses1.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个赋值
                            arrs2.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs2);
                        Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses1.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses1.get(i1).getCourseName());
                        courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        double result = num / count * 100;
                        int bfb = (int) result;
                        if (num == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                }
                return listVos;
            }
        } else {
            //此学校的方向列表
            List<Direction> directions = directionMapper.selectList(new QueryWrapper<Direction>().eq("school_id", schoolId));
            for (int i = 0; i < directions.size(); i++) {
                CourseListVo courseListVo = new CourseListVo();
                List<CourseVo> list = new ArrayList<>();
                Integer directionId = directions.get(i).getDirectionId();
                //查到方向名称
                Direction direction = directionMapper.selectById(directionId);
                //查到对应的课程列表  登陆进来的是老师，直接查全部课程
                List<Course> courses = courseMapper.selectListBySid(directionId, schoolId);
                //放到Vo课程中
                for (int i1 = 0; i1 < courses.size(); i1++) {
                    CourseVo courseVo = new CourseVo();
                    List<String> arrs = new ArrayList<>();
                    //字符串转换成数组
                    String directionId1 = courses.get(i1).getDirectionId();
                    String[] arr = directionId1.split(",");
                    for (int i2 = 0; i2 < arr.length; i2++) {
                        Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                        //一个一个添加值
                        arrs.add(direction1.getDirectionName());
                    }
                    courseVo.setDirectionName(arrs);
                    Professional professional = professionalMapper.selectById(courses.get(i1).getProfessionalId());
                    courseVo.setProfessionalName(professional.getProfessionalName());
                    courseVo.setCourseId(courses.get(i1).getCourseId());
                    courseVo.setCourseInfo(courses.get(i1).getCourseInfo());
                    courseVo.setCourseName(courses.get(i1).getCourseName());
                    courseVo.setCreateTime(courses.get(i1).getCreateTime());
                    courseVo.setCreateUser(courses.get(i1).getCreateUser());
                    courseVo.setLessonHours(courses.get(i1).getLessonHours());
                    courseVo.setUpdateTime(courses.get(i1).getUpdateTime());
                    courseVo.setUpdateUser(courses.get(i1).getUpdateUser());
                    //查全部视频个数
                    double count = videoMapper.selectCount(courses.get(i1).getCourseId());
                    //查学习完的个数   //当前登录用户
                    double num = videoMapper.selectNum(courses.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                    double result = num / count * 100;
                    int bfb = (int) result;
                    if (num == 0) {
                        courseVo.setCourseStatus(0);
                    } else {
                        courseVo.setCourseStatus(bfb);
                    }
                    list.add(courseVo);
                }
                courseListVo.setDirection(direction);
                courseListVo.setList(list);
                listVos.add(courseListVo);
            }
            return listVos;
        }
        return  null;
    }

    @Override
    public List<CourseListVo> getCourseListByDirectionId(Integer dId, Integer pId, String courseName) {
//        根据登录用户取到学校编号，班级编号
        Integer schoolId = ThreadLocalUtil.getUser().getSchoolId();
        //看登陆的是学生还是老师
        String userType = ThreadLocalUtil.getUser().getUserType();
        //查到是哪个班级
        Integer classId = userClassMapper.selectClassIdByUserId(ThreadLocalUtil.getUser().getUserId());
        //只有方向条件
        if (dId != null && pId == null && courseName == null) {
            //声明集合
            List<CourseListVo> listVos = new ArrayList<>();
            //声明对象
            CourseListVo courseListVo = new CourseListVo();
            //课程集合
            List<CourseVo> list = new ArrayList<>();
            //查方向
            Direction direction = directionMapper.selectById(dId);
            //判断是学生还是老师
            if (Integer.parseInt(userType) == 2) {
                //所包含did的课程   //哪个班级的课程
                List<Course> courses = courseMapper.selectListByDid(dId, classId);
                for (int i1 = 0; i1 < courses.size(); i1++) {
                    CourseVo courseVo = new CourseVo();
                    List<String> arrs = new ArrayList<>();
                    //字符串转换成数组
                    String directionId1 = courses.get(i1).getDirectionId();
                    String[] arr = directionId1.split(",");
                    for (int i2 = 0; i2 < arr.length; i2++) {
                        Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                        //一个一个赋值
                        arrs.add(direction1.getDirectionName());
                    }
                    courseVo.setDirectionName(arrs);
                    Professional professional = professionalMapper.selectById(courses.get(i1).getProfessionalId());
                    courseVo.setProfessionalName(professional.getProfessionalName());
                    courseVo.setCourseId(courses.get(i1).getCourseId());
                    courseVo.setCourseInfo(courses.get(i1).getCourseInfo());
                    courseVo.setCourseName(courses.get(i1).getCourseName());
                    courseVo.setCreateTime(courses.get(i1).getCreateTime());
                    courseVo.setCreateUser(courses.get(i1).getCreateUser());
                    courseVo.setLessonHours(courses.get(i1).getLessonHours());
                    courseVo.setUpdateTime(courses.get(i1).getUpdateTime());
                    courseVo.setUpdateUser(courses.get(i1).getUpdateUser());
                    //查全部视频个数
                    double count = videoMapper.selectCount(courses.get(i1).getCourseId());
                    //查学习完的个数
                    double num = videoMapper.selectNum(courses.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                    double result = num / count * 100;
                    int bfb = (int) result;
                    if (num == 0) {
                        courseVo.setCourseStatus(0);
                    } else {
                        courseVo.setCourseStatus(bfb);
                    }
                    list.add(courseVo);
                }
                courseListVo.setDirection(direction);
                courseListVo.setList(list);
                listVos.add(courseListVo);
                return listVos;
            } else {
                //所包含did的课程   //哪个班级的课程
                List<Course> courses = courseMapper.selectListBySid(dId, schoolId);
                for (int i1 = 0; i1 < courses.size(); i1++) {
                    CourseVo courseVo = new CourseVo();
                    List<String> arrs = new ArrayList<>();
                    //字符串转换成数组
                    String directionId1 = courses.get(i1).getDirectionId();
                    String[] arr = directionId1.split(",");
                    for (int i2 = 0; i2 < arr.length; i2++) {
                        Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                        //一个一个赋值
                        arrs.add(direction1.getDirectionName());
                    }
                    courseVo.setDirectionName(arrs);
                    Professional professional = professionalMapper.selectById(courses.get(i1).getProfessionalId());
                    courseVo.setProfessionalName(professional.getProfessionalName());
                    courseVo.setCourseId(courses.get(i1).getCourseId());
                    courseVo.setCourseInfo(courses.get(i1).getCourseInfo());
                    courseVo.setCourseName(courses.get(i1).getCourseName());
                    courseVo.setCreateTime(courses.get(i1).getCreateTime());
                    courseVo.setCreateUser(courses.get(i1).getCreateUser());
                    courseVo.setLessonHours(courses.get(i1).getLessonHours());
                    courseVo.setUpdateTime(courses.get(i1).getUpdateTime());
                    courseVo.setUpdateUser(courses.get(i1).getUpdateUser());
                    //查全部视频个数
                    double count = videoMapper.selectCount(courses.get(i1).getCourseId());
                    //查学习完的个数
                    double num = videoMapper.selectNum(courses.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                    double result = num / count * 100;
                    int bfb = (int) result;
                    if (num == 0) {
                        courseVo.setCourseStatus(0);
                    } else {
                        courseVo.setCourseStatus(bfb);
                    }
                    list.add(courseVo);
                }
                courseListVo.setDirection(direction);
                courseListVo.setList(list);
                listVos.add(courseListVo);
                return listVos;
            }

        }
        //只有类型条件
        if (dId == null && pId != null && courseName == null) {
            //学生走的
            if (Integer.parseInt(userType) == 2) {
                List<Course> courses = courseMapper.selectListByPid(pId, classId);
                if (courses.size() > 0) {
                    List<CourseListVo> listVos = new ArrayList<>();
                    List<Integer> arrs = new ArrayList<>();
                    //得到所有的方向
                    for (int i = 0; i < courses.size(); i++) {
                        String arr[] = courses.get(i).getDirectionId().split(",");
                        for (int i1 = 0; i1 < arr.length; i1++) {
                            int i2 = Integer.parseInt(arr[i1]);
                            arrs.add(i2);
                        }
                    }
                    // 使用 HashSet 去重
                    Set<Integer> uniqueDirections = new HashSet<>(arrs);
                    // 去重后的集合转换回 List
                    List<Integer> arrayList = new ArrayList<>(uniqueDirections);
                    for (int i = 0; i < arrayList.size(); i++) {
                        //声明对象
                        CourseListVo courseListVo = new CourseListVo();
                        Direction direction = directionMapper.selectById(arrayList.get(i));
                        //课程集合
                        List<CourseVo> list = new ArrayList<>();
                        //查到的课程列表根据类型ID和方向ID
                        List<Course> courses1 = courseMapper.selectListByPidAndDid(pId, direction.getDirectionId(), classId);
                        for (int i1 = 0; i1 < courses1.size(); i1++) {
                            CourseVo courseVo = new CourseVo();
                            List<String> arrs2 = new ArrayList<>();
                            //字符串转换成数组
                            String directionId1 = courses1.get(i1).getDirectionId();
                            String[] arr = directionId1.split(",");
                            for (int i2 = 0; i2 < arr.length; i2++) {
                                Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                                //一个一个赋值
                                arrs2.add(direction1.getDirectionName());
                            }
                            courseVo.setDirectionName(arrs2);
                            Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                            courseVo.setProfessionalName(professional.getProfessionalName());
                            courseVo.setCourseId(courses1.get(i1).getCourseId());
                            courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                            courseVo.setCourseName(courses1.get(i1).getCourseName());
                            courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                            courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                            courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                            courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                            courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                            //查全部视频个数
                            double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                            //查学习完的个数
                            double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                            double result = num / count * 100;
                            int bfb = (int) result;
                            if (num == 0) {
                                courseVo.setCourseStatus(0);
                            } else {
                                courseVo.setCourseStatus(bfb);
                            }
                            list.add(courseVo);
                        }
                        courseListVo.setDirection(direction);
                        courseListVo.setList(list);
                        listVos.add(courseListVo);
                    }
                    return listVos;
                }
            } else {
                List<Course> courses = courseMapper.selectListByPidSid(pId, schoolId);
                if (courses.size() > 0) {
                    List<CourseListVo> listVos = new ArrayList<>();
                    List<Integer> arrs = new ArrayList<>();
                    //得到所有的方向
                    for (int i = 0; i < courses.size(); i++) {
                        String arr[] = courses.get(i).getDirectionId().split(",");
                        for (int i1 = 0; i1 < arr.length; i1++) {
                            int i2 = Integer.parseInt(arr[i1]);
                            arrs.add(i2);
                        }
                    }
                    // 使用 HashSet 去重
                    Set<Integer> uniqueDirections = new HashSet<>(arrs);
                    // 去重后的集合转换回 List
                    List<Integer> arrayList = new ArrayList<>(uniqueDirections);
                    for (int i = 0; i < arrayList.size(); i++) {
                        //声明对象
                        CourseListVo courseListVo = new CourseListVo();
                        Direction direction = directionMapper.selectById(arrayList.get(i));
                        //课程集合
                        List<CourseVo> list = new ArrayList<>();
                        //查到的课程列表根据类型ID和方向ID
                        List<Course> courses1 = courseMapper.selectListByPidAndDidSid(pId, direction.getDirectionId(), schoolId);
                        for (int i1 = 0; i1 < courses1.size(); i1++) {
                            CourseVo courseVo = new CourseVo();
                            List<String> arrs2 = new ArrayList<>();
                            //字符串转换成数组
                            String directionId1 = courses1.get(i1).getDirectionId();
                            String[] arr = directionId1.split(",");
                            for (int i2 = 0; i2 < arr.length; i2++) {
                                Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                                //一个一个赋值
                                arrs2.add(direction1.getDirectionName());
                            }
                            courseVo.setDirectionName(arrs2);
                            Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                            courseVo.setProfessionalName(professional.getProfessionalName());
                            courseVo.setCourseId(courses1.get(i1).getCourseId());
                            courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                            courseVo.setCourseName(courses1.get(i1).getCourseName());
                            courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                            courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                            courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                            courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                            courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                            //查全部视频个数
                            double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                            //查学习完的个数
                            double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                            double result = num / count * 100;
                            int bfb = (int) result;
                            if (num == 0) {
                                courseVo.setCourseStatus(0);
                            } else {
                                courseVo.setCourseStatus(bfb);
                            }
                            list.add(courseVo);
                        }
                        courseListVo.setDirection(direction);
                        courseListVo.setList(list);
                        listVos.add(courseListVo);
                    }
                    return listVos;
                }
            }
        }
        //只有name条件
        if (dId == null && pId == null && courseName != null) {
            return this.getCourseListByName(courseName);
        }
        //类型和专业条件
        if (pId != null && dId != null && courseName == null) {
            if (Integer.parseInt(userType) == 2) {
                //查到的方向
                Direction direction = directionMapper.selectById(dId);
                List<Course> courses = courseMapper.selectListByPidAndDid(pId, dId, classId);
                if (courses.size() > 0) {
                    //声明返回集合
                    List<CourseListVo> listVos = new ArrayList<>();
                    List<CourseVo> list = new ArrayList<>();
                    CourseListVo courseListVo = new CourseListVo();
                    //放到Vo课程中
                    for (int i1 = 0; i1 < courses.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个添加值
                            arrs.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs);
                        Professional professional = professionalMapper.selectById(courses.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses.get(i1).getCourseName());
                        courseVo.setCreateTime(courses.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        double result = num / count * 100;
                        int bfb = (int) result;
                        if (num == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                    return listVos;
                }
            } else {
                //查到的方向
                Direction direction = directionMapper.selectById(dId);
                List<Course> courses = courseMapper.selectListByPidAndDidSid(pId, dId, schoolId);
                if (courses.size() > 0) {
                    //声明返回集合
                    List<CourseListVo> listVos = new ArrayList<>();
                    List<CourseVo> list = new ArrayList<>();
                    CourseListVo courseListVo = new CourseListVo();
                    //放到Vo课程中
                    for (int i1 = 0; i1 < courses.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个添加值
                            arrs.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs);
                        Professional professional = professionalMapper.selectById(courses.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses.get(i1).getCourseName());
                        courseVo.setCreateTime(courses.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        double result = num / count * 100;
                        int bfb = (int) result;
                        if (num == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                    return listVos;
                }
            }

            return null;
        }
        //方向和name条件
        if (pId == null && dId != null && courseName != null) {
            //声明集合
            List<CourseListVo> listVos = new ArrayList<>();
            //声明对象
            CourseListVo courseListVo = new CourseListVo();
            //课程集合
            List<CourseVo> list = new ArrayList<>();
            //查方向
            Direction direction = directionMapper.selectById(dId);
            if (Integer.parseInt(userType) == 2) {
                //所包含did的课程和name的课程
                List<Course> courses = courseMapper.selectListByNameAndDid(courseName, dId, classId);
                if (courses.size() > 0) {
                    for (int i1 = 0; i1 < courses.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个赋值
                            arrs.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs);
                        Professional professional = professionalMapper.selectById(courses.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses.get(i1).getCourseName());
                        courseVo.setCreateTime(courses.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        double result = num / count * 100;
                        int bfb = (int) result;
                        if (num == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                    return listVos;
                }
            } else {
                //所包含did的课程和name的课程
                List<Course> courses = courseMapper.selectListByNameAndDidSid(courseName, dId, schoolId);
                if (courses.size() > 0) {
                    for (int i1 = 0; i1 < courses.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个赋值
                            arrs.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs);
                        Professional professional = professionalMapper.selectById(courses.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses.get(i1).getCourseName());
                        courseVo.setCreateTime(courses.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        double result = num / count * 100;
                        int bfb = (int) result;
                        if (num == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                    return listVos;
                }
            }

            return null;
        }
        //类型和name条件
        if (pId != null && dId == null && courseName != null) {
            if (Integer.parseInt(userType) == 2) {
                //查到所有符合条件的课程
                List<Course> courses = courseMapper.selectListByNameAndPid(courseName, pId, classId);
                if (courses.size() > 0) {
                    List<CourseListVo> listVos = new ArrayList<>();
                    List<Integer> arrs = new ArrayList<>();
                    //得到所有的方向
                    for (int i = 0; i < courses.size(); i++) {
                        String arr[] = courses.get(i).getDirectionId().split(",");
                        for (int i1 = 0; i1 < arr.length; i1++) {
                            int i2 = Integer.parseInt(arr[i1]);
                            arrs.add(i2);
                        }
                    }
                    // 使用 HashSet 去重
                    Set<Integer> uniqueDirections = new HashSet<>(arrs);
                    // 去重后的集合转换回 List
                    List<Integer> arrayList = new ArrayList<>(uniqueDirections);
                    for (int i = 0; i < arrayList.size(); i++) {
                        //声明对象
                        CourseListVo courseListVo = new CourseListVo();
                        Direction direction = directionMapper.selectById(arrayList.get(i));
                        //课程集合
                        List<CourseVo> list = new ArrayList<>();
                        //查到的课程列表根据类型ID和方向ID和名字查询
                        List<Course> courses1 = courseMapper.selectListByNameAndPidAndDid(courseName, pId, arrayList.get(i), classId);
                        System.out.println(courses1);
                        for (int i1 = 0; i1 < courses1.size(); i1++) {
                            CourseVo courseVo = new CourseVo();
                            List<String> arrs2 = new ArrayList<>();
                            //字符串转换成数组
                            String directionId1 = courses1.get(i1).getDirectionId();
                            String[] arr = directionId1.split(",");
                            for (int i2 = 0; i2 < arr.length; i2++) {
                                Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                                //一个一个赋值
                                arrs2.add(direction1.getDirectionName());
                            }
                            courseVo.setDirectionName(arrs2);
                            Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                            courseVo.setProfessionalName(professional.getProfessionalName());
                            courseVo.setCourseId(courses1.get(i1).getCourseId());
                            courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                            courseVo.setCourseName(courses1.get(i1).getCourseName());
                            courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                            courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                            courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                            courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                            courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                            //查全部视频个数
                            double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                            //查学习完的个数
                            double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                            double result = num / count * 100;
                            int bfb = (int) result;
                            if (num == 0) {
                                courseVo.setCourseStatus(0);
                            } else {
                                courseVo.setCourseStatus(bfb);
                            }
                            list.add(courseVo);
                        }
                        courseListVo.setDirection(direction);
                        courseListVo.setList(list);
                        listVos.add(courseListVo);
                    }
                    return listVos;
                }
            } else {
                //查到所有符合条件的课程
                List<Course> courses = courseMapper.selectListByNameAndPidSid(courseName, pId, schoolId);
                if (courses.size() > 0) {
                    List<CourseListVo> listVos = new ArrayList<>();
                    List<Integer> arrs = new ArrayList<>();
                    //得到所有的方向
                    for (int i = 0; i < courses.size(); i++) {
                        String arr[] = courses.get(i).getDirectionId().split(",");
                        for (int i1 = 0; i1 < arr.length; i1++) {
                            int i2 = Integer.parseInt(arr[i1]);
                            arrs.add(i2);
                        }
                    }
                    // 使用 HashSet 去重
                    Set<Integer> uniqueDirections = new HashSet<>(arrs);
                    // 去重后的集合转换回 List
                    List<Integer> arrayList = new ArrayList<>(uniqueDirections);
                    for (int i = 0; i < arrayList.size(); i++) {
                        //声明对象
                        CourseListVo courseListVo = new CourseListVo();
                        Direction direction = directionMapper.selectById(arrayList.get(i));
                        //课程集合
                        List<CourseVo> list = new ArrayList<>();
                        //查到的课程列表根据类型ID和方向ID和名字查询
                        List<Course> courses1 = courseMapper.selectListByNameAndPidAndDidSid(courseName, pId, arrayList.get(i), schoolId);
                        System.out.println(courses1);
                        for (int i1 = 0; i1 < courses1.size(); i1++) {
                            CourseVo courseVo = new CourseVo();
                            List<String> arrs2 = new ArrayList<>();
                            //字符串转换成数组
                            String directionId1 = courses1.get(i1).getDirectionId();
                            String[] arr = directionId1.split(",");
                            for (int i2 = 0; i2 < arr.length; i2++) {
                                Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                                //一个一个赋值
                                arrs2.add(direction1.getDirectionName());
                            }
                            courseVo.setDirectionName(arrs2);
                            Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                            courseVo.setProfessionalName(professional.getProfessionalName());
                            courseVo.setCourseId(courses1.get(i1).getCourseId());
                            courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                            courseVo.setCourseName(courses1.get(i1).getCourseName());
                            courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                            courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                            courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                            courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                            courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                            //查全部视频个数
                            double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                            //查学习完的个数
                            double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                            double result = num / count * 100;
                            int bfb = (int) result;
                            if (num == 0) {
                                courseVo.setCourseStatus(0);
                            } else {
                                courseVo.setCourseStatus(bfb);
                            }
                            list.add(courseVo);
                        }
                        courseListVo.setDirection(direction);
                        courseListVo.setList(list);
                        listVos.add(courseListVo);
                    }
                    return listVos;
                }
            }

            return null;
        }
        //三个条件都有
        if (pId != null && dId != null && courseName != null) {
            if (Integer.parseInt(userType) == 2) {
                List<Course> courses = courseMapper.selectListByNameAndPidAndDid(courseName, pId, dId, classId);
                //方向
                Direction direction = directionMapper.selectById(dId);
                if (courses.size() > 0) {
                    //声明对象
                    CourseListVo courseListVo = new CourseListVo();
                    //课程集合
                    List<CourseVo> list = new ArrayList<>();
                    List<CourseListVo> listVos = new ArrayList<>();
                    //查到的课程列表根据类型ID和方向ID和名字查询
                    List<Course> courses1 = courseMapper.selectListByNameAndPidAndDid(courseName, pId, dId, classId);
                    for (int i1 = 0; i1 < courses1.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs2 = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses1.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个赋值
                            arrs2.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs2);
                        Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses1.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses1.get(i1).getCourseName());
                        courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        double result = num / count * 100;
                        int bfb = (int) result;
                        if (num == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                    return listVos;
                }
                return null;
            } else {
                List<Course> courses = courseMapper.selectListByNameAndPidAndDidSid(courseName, pId, dId, schoolId);
                //方向
                Direction direction = directionMapper.selectById(dId);
                if (courses.size() > 0) {
                    //声明对象
                    CourseListVo courseListVo = new CourseListVo();
                    //课程集合
                    List<CourseVo> list = new ArrayList<>();
                    List<CourseListVo> listVos = new ArrayList<>();
                    //查到的课程列表根据类型ID和方向ID和名字查询
                    List<Course> courses1 = courseMapper.selectListByNameAndPidAndDidSid(courseName, pId, dId, schoolId);
                    for (int i1 = 0; i1 < courses1.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs2 = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses1.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个赋值
                            arrs2.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs2);
                        Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses1.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses1.get(i1).getCourseName());
                        courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        double result = num / count * 100;
                        int bfb = (int) result;
                        if (num == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                    return listVos;
                }
                return null;
            }
        }
        //三个条件都没有
        if (pId == null && dId == null && courseName == null) {
            return this.getCourseList();
        }
        return null;
    }


    @Override
    public List<CourseListVo> getCourseListByName(String courseName) {
        //        根据登录用户取到学校编号，班级编号
        Integer schoolId = ThreadLocalUtil.getUser().getSchoolId();
        //看登陆的是学生还是老师
        String userType = ThreadLocalUtil.getUser().getUserType();
        //学生走的方法
        if (Integer.parseInt(userType) == 2) {
            //查到是哪个班级
            Integer classId = userClassMapper.selectClassIdByUserId(ThreadLocalUtil.getUser().getUserId());
            List<Course> courses = courseMapper.selectListByName(courseName, classId);
            if (courses.size() > 0) {
                List<CourseListVo> listVos = new ArrayList<>();
                List<Integer> arrs = new ArrayList<>();
                //得到所有的方向
                for (int i = 0; i < courses.size(); i++) {
                    String arr[] = courses.get(i).getDirectionId().split(",");
                    for (int i1 = 0; i1 < arr.length; i1++) {
                        int i2 = Integer.parseInt(arr[i1]);
                        arrs.add(i2);
                    }
                }
                // 使用 HashSet 去重
                Set<Integer> uniqueDirections = new HashSet<>(arrs);
                // 去重后的集合转换回 List
                List<Integer> arrayList = new ArrayList<>(uniqueDirections);
                for (int i = 0; i < arrayList.size(); i++) {
                    //声明对象
                    CourseListVo courseListVo = new CourseListVo();
                    Direction direction = directionMapper.selectById(arrayList.get(i));
                    //课程集合
                    List<CourseVo> list = new ArrayList<>();
                    //查到的课程列表根据类型ID和方向ID
                    List<Course> courses1 = courseMapper.selectListByNameAndDid(courseName, arrayList.get(i), classId);
                    for (int i1 = 0; i1 < courses1.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs2 = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses1.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个赋值
                            arrs2.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs2);
                        Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses1.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses1.get(i1).getCourseName());
                        courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        double result = num / count * 100;
                        int bfb = (int) result;
                        if (num == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                }
                return listVos;
            }
        } else {
            //不是学生走的方法
            List<Course> courses = courseMapper.selectListByNameSid(courseName, schoolId);
            if (courses.size() > 0) {
                List<CourseListVo> listVos = new ArrayList<>();
                List<Integer> arrs = new ArrayList<>();
                //得到所有的方向
                for (int i = 0; i < courses.size(); i++) {
                    String arr[] = courses.get(i).getDirectionId().split(",");
                    for (int i1 = 0; i1 < arr.length; i1++) {
                        int i2 = Integer.parseInt(arr[i1]);
                        arrs.add(i2);
                    }
                }
                // 使用 HashSet 去重
                Set<Integer> uniqueDirections = new HashSet<>(arrs);
                // 去重后的集合转换回 List
                List<Integer> arrayList = new ArrayList<>(uniqueDirections);
                for (int i = 0; i < arrayList.size(); i++) {
                    //声明对象
                    CourseListVo courseListVo = new CourseListVo();
                    Direction direction = directionMapper.selectById(arrayList.get(i));
                    //课程集合
                    List<CourseVo> list = new ArrayList<>();
                    //查到的课程列表根据类型ID和方向ID
                    List<Course> courses1 = courseMapper.selectListByNameAndDidSid(courseName, arrayList.get(i), schoolId);
                    for (int i1 = 0; i1 < courses1.size(); i1++) {
                        CourseVo courseVo = new CourseVo();
                        List<String> arrs2 = new ArrayList<>();
                        //字符串转换成数组
                        String directionId1 = courses1.get(i1).getDirectionId();
                        String[] arr = directionId1.split(",");
                        for (int i2 = 0; i2 < arr.length; i2++) {
                            Direction direction1 = directionMapper.selectById(Integer.parseInt(arr[i2]));
                            //一个一个赋值
                            arrs2.add(direction1.getDirectionName());
                        }
                        courseVo.setDirectionName(arrs2);
                        Professional professional = professionalMapper.selectById(courses1.get(i1).getProfessionalId());
                        courseVo.setProfessionalName(professional.getProfessionalName());
                        courseVo.setCourseId(courses1.get(i1).getCourseId());
                        courseVo.setCourseInfo(courses1.get(i1).getCourseInfo());
                        courseVo.setCourseName(courses1.get(i1).getCourseName());
                        courseVo.setCreateTime(courses1.get(i1).getCreateTime());
                        courseVo.setCreateUser(courses1.get(i1).getCreateUser());
                        courseVo.setLessonHours(courses1.get(i1).getLessonHours());
                        courseVo.setUpdateTime(courses1.get(i1).getUpdateTime());
                        courseVo.setUpdateUser(courses1.get(i1).getUpdateUser());
                        //查全部视频个数
                        double count = videoMapper.selectCount(courses1.get(i1).getCourseId());
                        //查学习完的个数
                        double num = videoMapper.selectNum(courses1.get(i1).getCourseId(), ThreadLocalUtil.getUser().getUserId());
                        if (count == 0) {
                            courseVo.setCourseStatus(0);
                        } else {
                            double result = num / count * 100;
                            int bfb = (int) result;
                            courseVo.setCourseStatus(bfb);
                        }
                        list.add(courseVo);
                    }
                    courseListVo.setDirection(direction);
                    courseListVo.setList(list);
                    listVos.add(courseListVo);
                }
                return listVos;
            }
        }
        return null;
    }

    //删除课程
    @Override
    public int deleteCourse(Integer courseId) {
        Integer i = courseMapper.selectClassMenuByCourseId(courseId);
        if (i>0){
            return 1;
        }
        QueryWrapper<Chapter> chapterQueryWrapper=new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        List<Chapter> chapters = chapterMapper.selectList(chapterQueryWrapper);
        for (Chapter chapter : chapters) {
            QueryWrapper<Section> sectionQueryWrapper = new QueryWrapper<>();
            sectionQueryWrapper.eq("chapter_id",chapter.getChapterId());
            List<Section> sections = sectionMapper.selectList(sectionQueryWrapper);
            for (Section section : sections) {
                sectionMapper.deleteById(section.getSectionId());
            }
            chapterMapper.deleteById(chapter.getChapterId());
        }
        courseMapper.deleteById(courseId);
        return 0;
    }
     @Override
    public List<String> getCourseLists(int userId) {
        List<String> courseLists = courseMapper.getCourseLists(userId);
        if (courseLists!=null){
            return courseLists;
        }
        return null;
}

    //根据课程id修改课程详情信息
    @Override
    public int reviseurse(Course course) {
        int insert = courseMapper.updateById(course);
        return insert;
    }
}
